﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic.BusinessEntity.Entities;
using BankApplication.BusinessLogic;

namespace BankApplication.UI
{
	public partial class ReplenishAccountForm : Form
	{
		public ReplenishAccountForm(int accountID)
		{
			InitializeComponent();

			this.accountID = accountID;
		}

		private void ValidateForm()
		{

			decimal moneyToAdd;

			try
			{
				moneyToAdd = Convert.ToDecimal(MoneyToAddTextBox.Text);
			}
			catch (Exception)
			{
				throw new ValidationException(String.Format("Incorrect value for the sum of money to add: {0}!" +
					" It must be a valid decimal value! (e.g. 100,34)", MoneyToAddTextBox.Text));
			}

			if (moneyToAdd <= 0)
			{
				throw new ValidationException("The balance change can only be a non-negative decimal number!");
			}
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			try
			{
				ValidateForm();

				Decimal sum = Convert.ToDecimal(MoneyToAddTextBox.Text);

				new ServerInteractionExceptionCatcher().Execute(() =>
				{
					AccountsManager.ReplenishAccount(accountID, sum, SessionManager.Instance.SessionTicket);

					Close();
				});
			}
			catch (ValidationException exception)
			{
				MessageBox.Show(exception.Message, "Form validation failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void CancelButton_Click(object sender, EventArgs e)
		{
			Close();
		}


		private int accountID;
	}
}
